package com.itheima.contorller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.itheima.common.R;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
   private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //获取验证码
    @PostMapping("/sendMsg")
    public R<String> login(@RequestBody User user, HttpSession session){
        String phone = user.getPhone();

        if (Strings.isNotBlank(phone)){
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code:"+code);
            //通过腾讯运的短信服务发送短信


            //验证码存入Session
//            session.setAttribute(phone,code);
            //将生产的验证码缓存放到redis中，并设置有效时间为五分钟
             redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return R.success("短信发送成功");
        }



  return R.error("短信发送失败");
    }
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map,HttpSession session){
        //获取手机号和验证码
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
       //取出session中的保存的验证码
//        String s = session.getAttribute(phone).toString();
      //从redis缓存中取出验证码
        Object s = redisTemplate.opsForValue().get(phone);
        if (s!=null && code.equals(s)){
//            LambdaQueryWrapper<User> userLambdaQueryChainWrapper = new LambdaQueryWrapper<>();
//
//            userLambdaQueryChainWrapper.eq(User::getPhone,phone);
//            User one = userService.getOne(userLambdaQueryChainWrapper);

            LambdaQueryWrapper<User> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            objectLambdaQueryWrapper.eq(User::getPhone,phone);
            User one = userService.getOne(objectLambdaQueryWrapper);
            if (one==null){
                User user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);

            }
            session.setAttribute("user",one.getId());
            //如果用户登入成功，删除redis缓存中的验证码
            redisTemplate.delete(phone);
          return R.success(one);


        }
         return R.error("登入失败");


    }
    @PostMapping("/loginout")
    public R<String> logout(HttpServletRequest request){
        //清理session中的用户id
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }
}
